package cn.com.headfree.point.dao.impl;

import java.sql.Timestamp;
import java.util.*;

import cn.com.headfree.entity.*;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.point.dao.PointRuleDao;
import org.springframework.stereotype.Repository;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;


@Repository("pointRuleDao")
public class PointRuleDaoImpl extends BaseDaoImpl<TPointRuleInfo> implements PointRuleDao {

	@Override
	public void savePointRuleInfo(TPointRuleInfo pointRuleInfo) {
		this.save(pointRuleInfo);
	}

	@Override
	public void getPagePoingRuleInfo(Page<TPointRuleInfo> page, TPointRuleInfo pointRuleInfo,User user) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		String sql = "select t.* from t_point_rule_info t where t.del_sign = 0 " ;
		StringBuilder sqlBuilder = new StringBuilder(sql);
		if (pointRuleInfo.getYear() != null) {
			sqlBuilder.append(" AND t.year= ? ");
			arrayList.add(pointRuleInfo.getYear());
		}
		if(pointRuleInfo.getState() != null){
			sqlBuilder.append(" AND t.state = ? ");
			arrayList.add(pointRuleInfo.getState());
		}
		if(!StringUtil.isNullOrEmpty(pointRuleInfo.getRuleName())){
			sqlBuilder.append(" AND t.rule_name LIKE concat('%',?,'%') ");
			arrayList.add(pointRuleInfo.getRuleName());
		}
		if(user!=null) {
			sqlBuilder.append(" AND t.org_id= ? ");
			arrayList.add(user.getBaseOrgId());
		}
		sqlBuilder.append(" order by create_time desc ");
		this.findPageBySql(TPointRuleInfo.class, sqlBuilder.toString(), page, arrayList.toArray());
	}

	@Override
	public void updatePointRuleInfoByState(TPointRuleInfo pointRuleInfo) {
		String sql = "update t_point_rule_info t set t.state = '0',t.updater_id=#{updater_id,jdbcType=VARCHAR},t.updater_name=#{updater_name,jdbcType=VARCHAR},t.update_time=#{update_time,jdbcType=DATE} where t.state = '1' AND t.del_sign = 0 and t.org_id=#{org_id,jdbcType=VARCHAR} " ;
		Map<String,Object> params = new HashMap<>();
		params.put("updater_id", pointRuleInfo.getUpdaterId());
		params.put("updater_name", pointRuleInfo.getUpdaterName());
		params.put("update_time", new Timestamp(new Date().getTime()));
		params.put("org_id", pointRuleInfo.getOrgId());
		this.executeSql(sql,params);
	}

	@Override
	public void updatePointRuleInfoById(TPointRuleInfo pointRuleInfo) {
		String sql = "update t_point_rule_info t set t.state = '1',t.updater_id=#{updater_id,jdbcType=VARCHAR},t.updater_name=#{updater_name,jdbcType=VARCHAR},t.update_time=#{update_time,jdbcType=DATE} " +
				"where t.state = '0' AND t.del_sign = 0 AND t.id=#{id,jdbcType=VARCHAR} and t.org_id=#{org_id,jdbcType=VARCHAR}" ;
		Map<String,Object> params = new HashMap<>();
		params.put("updater_id", pointRuleInfo.getUpdaterId());
		params.put("updater_name", pointRuleInfo.getUpdaterName());
		params.put("update_time", new Timestamp(new Date().getTime()));
		params.put("id",pointRuleInfo.getId());
		params.put("org_id", pointRuleInfo.getOrgId());
		this.executeSql(sql,params);
	}
	

	@Override
	public List<TPointRuleInfo> findStartUsePointRuleInfoList(String orgId) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		String sql = "select t.* from t_point_rule_info t where t.state = 1 and t.del_sign = 0 ";
		if(!StringUtil.isNullOrEmpty(orgId)) {
			sql+=" and t.org_id=? ";
			arrayList.add(orgId);
		}
		return this.findAllBySQL(TPointRuleInfo.class, sql,null,false,arrayList.toArray());
	}

}
